package Epic;

import java.util.StringTokenizer;

/**
 * From a given string, replace all instances of 'a' with 'one' and 'A' with 'ONE'. 

	Example Input: "A boy is playing in a garden" 

	Example Output: "ONE boy is playing in one garden" 
 * @author Ray
 *
 */
public class Replace_a_with_one {
	
	public static String replaceWords(String origin, String word, String updateWord){
		StringBuilder sb = new StringBuilder();
		StringTokenizer st = new StringTokenizer(origin,"+-*(),. ",true);
		while(st.hasMoreTokens()){
			String cur = st.nextToken();
			if(cur.equals(word)){
				sb.append(updateWord);
			}
			else{
				sb.append(cur);
			}
		}
		return sb.toString();
	}
	
	public static String replace(String s){
		if(s==null || s.length()==0) return "";
		StringBuilder sb = new StringBuilder();
		String[] str = s.split(" ");
		for(int i=0;i<str.length;++i){
			if(str[i].equals("A"))
				sb.append("ONE");
			else if(str[i].equals("a"))
				sb.append("one");
			else 
				sb.append(str[i]);
			if(i<str.length-1)
				sb.append(" ");
		}
		return sb.toString();
	}
	

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String test = "A boy is playing in a garden";
		System.out.println(replace(test));
		System.out.println(replaceWords(test, "A", "ONE"));
	}

}
